package com.hopedove.quanlitycheck.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hopedove.base.BaseActivity;
import com.hopedove.net.ErrResponse;
import com.hopedove.net.OkResponse;
import com.hopedove.net.domain.Communication;
import com.hopedove.quanlitycheck.R;
import com.hopedove.quanlitycheck.domain.net.QualityCheckLogin;
import com.hopedove.quanlitycheck.utils.QualitySharedPrefUtil;
import com.hopedove.utils.CommonUtil;
import com.hopedove.utils.ToastUtil;
import com.hopedove.view.SimpleDialog;

import org.json.JSONObject;

public class LoginActivity extends BaseActivity implements OnClickListener {

    private Button loginBtn;
    private EditText accountEt;
    private EditText passwordEt;
    private TextView versionTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.quality_login_activity);
        initView();
        initEvent();
    }

    private void initView() {
        loginBtn = findViewById(R.id.login_loginBtn);
        accountEt = findViewById(R.id.login_accountEt);
        passwordEt = findViewById(R.id.login_passwordEt);
        versionTv = findViewById(R.id.versionTv);

        accountEt.setFocusable(true);
        accountEt.setFocusableInTouchMode(true);

        accountEt.requestFocus();
        accountEt.requestFocusFromTouch();
        accountEt.findFocus();

        versionTv.setText("版本：V" + CommonUtil.getAppVersionName(this));
    }

    private void initEvent() {
        loginBtn.setOnClickListener(this);

        accountEt.setOnKeyListener((v, keyCode, event) -> {
            if (event != null
                    && event.getKeyCode() == KeyEvent.KEYCODE_ENTER
                    && event.getAction() == KeyEvent.ACTION_UP) {
                startLogin();
                return true;
            }
            return false;
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.login_loginBtn) {
            startLogin();
        }
    }

    private void startLogin() {
        String account = accountEt.getText().toString().trim();
        String password = CommonUtil.MD5(passwordEt.getText().toString().trim());

        if (TextUtils.isEmpty(account)) {
            SimpleDialog.show(this, "账号不能为空，请重新输入！");
            return;
        }
        login(account, password);
    }

    /**
     * 登录
     */
    private void login(String account, String password) {
        showWait();

        QualityCheckLogin qualityCheckLogin = new QualityCheckLogin(account, password);
        Communication.asyncRequest(qualityCheckLogin, r -> {
            stopWait();
            if (r instanceof OkResponse) {
                try {
                    JSONObject jsonObj = new JSONObject(((OkResponse) r).getData());
                    JSONObject data = jsonObj.optJSONObject("DATA");

                    QualitySharedPrefUtil.putValue(this, QualitySharedPrefUtil.USERBEAN, data.toString());
                    String username = data.optString("XM");
                    QualitySharedPrefUtil.putValue(this, QualitySharedPrefUtil.XM, username);
                    String JSMC = data.optString("JSMC");
                    QualitySharedPrefUtil.putValue(this, QualitySharedPrefUtil.JSMC, JSMC);
                    startActivity(new Intent(this, MainActivity2.class));
                } catch (Exception e) {
                    ToastUtil.longToast(this, "数据解析异常");
                }
            } else if (r instanceof ErrResponse) {
                SimpleDialog.show(this, ((ErrResponse) r).getErrText());
            }
        });
    }

}
